local function get_layout_config()
    local width = vim.o.columns
    local height = vim.o.lines
    -- 如果宽度大于高度的两倍，使用左右布局
    if width > height * 2 then
        return {
            horizontal = {
                width = 0.9,
                height = 0.8,
                preview_width = 0.6,
            },
        }
    else
        return {
            vertical = {
                width = 0.9,
                height = 0.9,
                preview_height = 0.5,
            },
        }
    end
end

local M = {
    "nvim-telescope/telescope.nvim",
    tag = "0.1.8",
    dependencies = { "nvim-lua/plenary.nvim" },
    cmd = { "Telescope" },
    --[[ keys = {
        {
            "<leader>lr",
            "<cmd>Telescope lsp_references<cr>",
            desc = "引用",
        },
        {
            "<leader>sf",
            function()
                local layout_config = get_layout_config()
                require("telescope.builtin").find_files {
                    layout_strategy = next(layout_config),
                    layout_config = layout_config[next(layout_config)],
                }
            end,
            desc = "search files",
        },
        {
            "?",
            function()
                local layout_config = get_layout_config()
                require("telescope.builtin").keymaps {
                    layout_strategy = next(layout_config),
                    layout_config = layout_config[next(layout_config)],
                }
            end,
            desc = "快捷键",
        },
        {
            "<leader>ss",
            function()
                local layout_config = get_layout_config()
                require("telescope.builtin").live_grep {
                    layout_strategy = next(layout_config),
                    layout_config = layout_config[next(layout_config)],
                }
            end,
            desc = "grep search",
        },
        {
            "<leader>fr",
            function()
                local layout_config = get_layout_config()
                require("telescope.builtin").oldfiles {
                    layout_strategy = next(layout_config),
                    layout_config = layout_config[next(layout_config)],
                }
            end,
            desc = "resume",
        },
        {
            "<leader>bs",
            function()
                local layout_config = get_layout_config()
                require("telescope.builtin").buffers {
                    layout_strategy = next(layout_config),
                    layout_config = layout_config[next(layout_config)],
                    sort_lastused = true,
                    ignore_current_buffer = true,
                }
            end,
            desc = "switch buffer",
        },
        {
            "<leader>fc",
            function()
                local layout_config = get_layout_config()
                require("telescope").commands {
                    layout_strategy = next(layout_config),
                    layout_config = layout_config[next(layout_config)],
                }
            end,
            desc = "commands",
        },
        {
            "<leader>fh",
            function()
                local layout_config = get_layout_config()
                require("telescope").help_tags {
                    layout_strategy = next(layout_config),
                    layout_config = layout_config[next(layout_config)],
                }
            end,
            desc = "help_tags",
        },
        {
            "<leader>fz",
            function()
                local layout_config = get_layout_config()
                require("telescope.builtin").builtin {
                    layout_strategy = next(layout_config),
                    layout_config = layout_config[next(layout_config)],
                }
            end,
            desc = "telescope",
        },
        {
            "<leader>lpd",
            function()
                local layout_config = get_layout_config()
                require("telescope.builtin").lsp_document_symbols {
                    layout_strategy = next(layout_config),
                    layout_config = layout_config[next(layout_config)],
                }
            end,
            desc = "lsp document",
        },
        {
            "<leader>lpo",
            function()
                local layout_config = get_layout_config()
                require("telescope.builtin").lsp_outgoing_calls {
                    layout_strategy = next(layout_config),
                    layout_config = layout_config[next(layout_config)],
                }
            end,
            desc = "lsp picker outgoing_calls",
        },
        {
            "<leader>lpi",
            function()
                local layout_config = get_layout_config()
                require("telescope.builtin").lsp_incoming_calls {
                    layout_strategy = next(layout_config),
                    layout_config = layout_config[next(layout_config)],
                }
            end,
            desc = "lsp picker incoming_calls",
        },
    }, ]]
    config = function()
        local telescope = require "telescope"
        local actions = require "telescope.actions"

        telescope.setup {
            defaults = {
                mappings = {
                    i = {
                        ["<C-j>"] = actions.move_selection_next,
                        ["<C-k>"] = actions.move_selection_previous,
                    },
                },
                file_ignore_patterns = {
                    "pnpm-lock.yaml",
                    "node_modules/*",
                    "go.mod",
                    "go.sum",
                    "uni_modules/*",
                },
                entry_prefix = "   ",
                initial_mode = "insert",
                selection_strategy = "reset",
                path_display = { "smart" },
                color_devicons = true,
                set_env = { ["COLORTERM"] = "truecolor" },
                sorting_strategy = nil,
                layout_strategy = nil,
                layout_config = {},
                vimgrep_arguments = {
                    "rg",
                    "--color=never",
                    "--no-heading",
                    "--with-filename",
                    "--line-number",
                    "--column",
                    "--smart-case",
                    -- "--hidden",
                    "--glob=!.git/",
                },
            },
        }
    end,
}

return M
